﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MvcEngine.Blog.Models.Entities;
using MvcEngine.Core.Utils;
using MvcEngine.Blog.Repositories;
using MvcEngine.Core;
using MvcEngine.Core.Pagination;
using MvcEngine.Core.Helpers;
using MvcEngine.Core.Repositories;

namespace MvcEngine.Blog.Managers
{
    public class BlogPostTagManager : IBlogPostTagManager
    {
        private readonly IBlogTagRepository tagRepository = null;

        public BlogPostTagManager(IBlogTagRepository tagRepository)
        {
            this.tagRepository = tagRepository;
        }

        public static IBlogPostTagManager Current 
        {
            get { return IoC.Resolve<IBlogPostTagManager>(); }
        }

        public IList<BlogTagBase> GetAll(Guid blogId)
        {
            return CacheManager.Current.FromCache(string.Format(BlogCacheKeys.TagKeys.TagList, blogId), BlogCacheKeys.TagKeys.Category,
                () => tagRepository.GetAll(blogId));
        }

        public IPagination<BlogTagBase> Get(Guid blogId, int pageNumber, int pageSize)
        {
            return CacheManager.Current.FromCache(string.Format(BlogCacheKeys.TagKeys.Paging, blogId, pageNumber, pageSize), BlogCacheKeys.TagKeys.Category,
                    () => tagRepository.GetAll(blogId, pageNumber, pageSize));
        }

        public IList<BlogTagBase> GetAllByPost(Guid postId)
        {
            return CacheManager.Current.FromCache(string.Format(BlogCacheKeys.TagKeys.PostTags, postId), BlogCacheKeys.TagKeys.Category,
                () => tagRepository.GetAllByPost(postId));
        }

        public BlogTagBase Get(Guid id)
        {
            return CacheManager.Current.FromCache(string.Format(BlogCacheKeys.TagKeys.TagId, id), BlogCacheKeys.TagKeys.Category,
                () => tagRepository.Get(id));
        }

        public void Save(BlogTagBase tag)
        {
            Guard.ThrowIfEmptyGuid(tag.BlogId);
            bool isUpdated = true;
            using (var unitOfWork = UnitOfWorkHelper.Get())
            {
                BlogTagBase tagToSave = null;
                if (tag.Id != Guid.Empty)
                {
                    tagToSave = tagRepository.Get(tag.Id);
                }

                if (tagToSave == null)
                {
                    tagToSave = new BlogTagBase { Id = Guid.NewGuid() };
                    tagToSave.CreatedDate = DateTime.Now;
                    isUpdated = false;
                }

                tagToSave.ModifiedDate = DateTime.Now;
                tagToSave.Name = tag.Name;
                tagToSave.Slug = tag.Slug;
                tagToSave.BlogId = tag.BlogId;

                if (isUpdated)
                {
                    tagRepository.Update(tagToSave);
                }
                else
                {
                    tagRepository.Add(tagToSave);
                }
                unitOfWork.Commit();
            }
            CacheManager.Current.ClearCache(BlogCacheKeys.TagKeys.Category);
        }

        public void Delete(Guid id)
        {
            tagRepository.Delete(id);
            CacheManager.Current.ClearCache(BlogCacheKeys.TagKeys.Category);
        }
    }
}
